package com.itheima.app.exception;

import com.itheima.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice  // 全局异常处理器
public class GlobalExceptionAdvice {

    //处理自定义的异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerBusinessException(Exception exception) {
        //打印异常
        exception.printStackTrace();

        return ResponseEntity.status(500).body(exception.getMessage());
    }

    // 捕获exception异常
    @ExceptionHandler
    public ResponseEntity exceptionHandlerMethod(Exception ex){
        // 将异常信息输出到控制台
        ex.printStackTrace();
        // 友情提示
        return ResponseEntity.status(500).body(ErrorResult.error());
    }
}